package com.huawei.petSystemdemo.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.bean.Shopping;
import com.huawei.petSystemdemo.utils.MyRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class ShoppingRepository {

    public static void getShoppingByUserId(int userId, Consumer<List<Shopping>> callback) {
        Map<String, Object> resBody = new HashMap<>();
        resBody.put("userId", userId);
        MyRequest. get("/shopping/getAll", resBody, json -> {
            JSONObject object = JSON.parseObject(json);
            boolean sucess = object.getBoolean("success");
            if(sucess == true) {
                List<Shopping> data = object.getJSONArray("data").toJavaList(Shopping.class);
                callback.accept(data);
            } else {
                System.out.println("wrong");
            }
        });
    }
}
